package com.ku.consumerpay.service.mq;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Component;

/**
 * @program: rocketMQKill
 * @description:  这个MQ用于用户账户扣减成功后发送消息到Order ,改变订单的状态
 * @author: JokerKu
 * @create: 2020-05-06 23:32
 **/
@Component
public class SyncProducer {

    private DefaultMQProducer producer ;

    private static final String NAME_SERVER = "192.168.43.10:9876;192.168.43.9:9876";

    private static final String PRODUCER_GROUP_NAME = "callback_pay_producer_group";

    private SyncProducer() throws MQClientException {
        this.producer = new DefaultMQProducer(PRODUCER_GROUP_NAME);
        this.producer.setNamesrvAddr(NAME_SERVER);
        this.producer.setRetryTimesWhenSendFailed(3);
        this.producer.start();
    }

    public SendResult sendMessage(Message message){
        SendResult sendResult = null;
        try {
            sendResult = this.producer.send(message);
        } catch (MQClientException | MQBrokerException | InterruptedException | RemotingException e) {
            e.printStackTrace();
        }
        return sendResult;
    }

    public void shutdown() {
        this.producer.shutdown();
    }
}
